﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Assessment.Management.SharedComponent.Params.ManagerParam;
using Assessment.Management.SharedComponent.Constants;
using Assessment.Management.BIZ;
using Assessment.Management.SharedComponent.Entities;

namespace Assessment.Management.UI.Admin.UserControls.Subjects
{
    public partial class SubjectDisplay : System.Web.UI.UserControl
    {
        #region events
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                string id = GetParam();
                hdID.Value = GetParam();
                if (!string.IsNullOrEmpty(id))
                {
                    BindData(id);
                }
                else
                {
                    GotoViewPage();
                }
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Response.Redirect("/UI/Admin/UniAdmin.aspx?do=subjectedit&id=" + hdID.Value);
        }

        protected void btnExit_Click(object sender, EventArgs e)
        {
            Response.Redirect("/UI/Admin/UniAdmin.aspx?do=subjectmanager");
        }
        #endregion

        #region methods
        private void GotoViewPage()
        {
            Response.Redirect("/UI/Admin/UniAdmin.aspx?do=subjectmanager");
        }

        private void BindData(string id)
        {
            SubjectParam param = GetSubjectByID(id);
            BindItemToForm(param);
        }

        private SubjectParam GetSubjectByID(string id)
        {
            SubjectParam param = new SubjectParam(FunctionType.GetSubjectToEditByCode);
            param.Subject = new Subject() { ID = int.Parse(id) };
            MainController.Provider.Execute(param);
            return param;
        }

        private void BindItemToForm(SubjectParam param)
        {
            txtSubjectID.Text = param.Subject.ID.ToString();
            txtCredit.Text = param.Subject.Credit.ToString();
            txtSubjectName.Text = param.Subject.SubjectName;
        }

        private string GetParam()
        {
            return Request.QueryString["id"];
        }
        #endregion
    }
}